package com.easy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ImageUtil {

    private  static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    private static final String BUCKET_NAME = "easy-travel";
    private static final String ACCESS_KEY_ID = "LTAI5t8RFSM1xrhLm7mnfect";
    private static final String ACCESS_KEY_SECRET = "DTExL1nL8beTeYwuBrD8byBr5kZob7";
//private static final String ENDPOINT = "oss-cn-hangzhou.aliyuncs.com";
//    private static final String BUCKET_NAME = "wyyy-zlich20k";
//    private static final String ACCESS_KEY_ID = "LTAI5tD9JBBgrU6ro7jf1fYd";
//    private static final String ACCESS_KEY_SECRET = "TNI4rM3zcQKOTYUORju4mITyDr6qav";

    // 创建OSS客户端实例
    public static final OSS ossClient = new OSSClientBuilder()
            .build("http://" + ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

    // 上传文件方法
    public static String upload(InputStream is, String fileName){
        // 生成唯一文件名
        String newFileName = UUID.randomUUID().toString()
                + fileName.substring(fileName.lastIndexOf("."));

        // 创建上传请求
        PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, newFileName, is);

        // 执行上传操作
        PutObjectResult result = ossClient.putObject(request);

        // 返回文件访问URL
        return "https://" + BUCKET_NAME + "." + ENDPOINT + "/" + newFileName;
    }

//    public static List<String> getFileList(){
//        List<String> list=new ArrayList<>();
//        ListObjectsRequest request = new ListObjectsRequest(BUCKET_NAME);
//        request.setMarker(null);
//        request.setMaxKeys(1000);
//       ObjectListing objectListing = ossClient.listObjects(request);
//       for(OSSObjectSummary objectSummary : objectListing.getObjectSummaries()){
//           list.add(objectSummary.getKey());
//       }
//        return list;
//    }
//    public static void delete(String fileName){
//        ossClient.deleteObject(BUCKET_NAME, fileName);
//    }
    /**
     * 删除OSS中的单个文件
     * @param objectName 文件名
     */
    public static void delete(String objectName) {
        ossClient.deleteObject(BUCKET_NAME, objectName);
    }

    /**
     * 批量删除OSS中的文件
     * @param objectNames 文件名列表
     */
    public static void batchDelete(List<String> objectNames) {
        // 每次最多删除1000个文件
        final int MAX_KEYS = 1000;
        for (int i = 0; i < objectNames.size(); i += MAX_KEYS) {
            int endIndex = Math.min(i + MAX_KEYS, objectNames.size());
            List<String> subList = objectNames.subList(i, endIndex);

            DeleteObjectsRequest request = new DeleteObjectsRequest(BUCKET_NAME);
            request.setKeys(subList);
            ossClient.deleteObjects(request);
        }
    }

    /**
     * 获取OSS中所有文件名
     * @return 文件名列表
     */
    public static List<String> listAllObjects() {
        List<String> objectNames = new ArrayList<>();
        ObjectListing objectListing = ossClient.listObjects(BUCKET_NAME);
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            objectNames.add(objectSummary.getKey());
        }

        // 如果还有更多文件，继续获取
        while (objectListing.isTruncated()) {
            objectListing = ossClient.listObjects(String.valueOf(objectListing));
            for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                objectNames.add(objectSummary.getKey());
            }
        }

        return objectNames;
    }
    public static void main(String[] args) {

//        try {
////            MultipartFile mfile=null;
////            mfile.getInputStream();
//            File file=new File("D:\\1111.jpg");
//            InputStream is=new FileInputStream(file);
//           String newFileName= upload(is,file.getName());
//            System.out.println(newFileName);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(getFileList());
    }
}
